﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace Expressing_Label
{
    public partial class InformationMiForm : Form
    {
        public string c_FileName;
        public int c_Gender;
        public int c_Department;
        private bool c_bMoving = false;
        private Point c_pStart = new Point();
        private Point[] poin = new Point[8];
        private bool m_TextFocus = false;           //同步输入框获取焦点标记
        private Bitmap c_bmpFrmBackground = null;
        private Bitmap c_bmpFrmBackgroundCache = null;

        public InformationMiForm()
        {
            InitializeComponent();
            this.Opacity = 50;

            foreach (Control c in this.Controls)
                c.Enter += new System.EventHandler(this.Control_Enter);

            poin[0] = new Point(0, 10);
            poin[1] = new Point(Width / 2 - 20, 10);
            poin[2] = new Point(Width / 2, 0);
            poin[3] = new Point(Width / 2 + 20, 10);
            poin[4] = new Point(Width, 10);
            poin[5] = new Point(Width, Height);
            poin[6] = new Point(0, Height);
            poin[7] = new Point(0, 10);

            IntPtr hRgn = CreatePolygonRgn(ref poin[0], 16, 1);
            SetWindowRgn(Handle, hRgn, true);

            //Load comobox NameItem Data
            for (int i = 0; i < PublicResources.g_NameItem.Count; i++)
                this.cmbxCharacter.Items.Add(PublicResources.g_NameItem[i]);
                
            //Load Comobox DepartmentItem Data
            for (int i = 0; i < PublicResources.g_DepartmentItem.Count; i++)
                this.DepartmentComboBox.Items.Add(PublicResources.g_DepartmentItem[i]);

            //Initialize the BackgroundImage
            c_bmpFrmBackground = new Bitmap(this.Width, this.Height);
            c_bmpFrmBackgroundCache = new Bitmap(this.Width, this.Height);
            Graphics gra = Graphics.FromImage(c_bmpFrmBackgroundCache);
            gra.Clear(Color.WhiteSmoke);
            Pen p = new Pen(Color.Purple, 4);
            for (int i = 0; i < 7; i++)
            {
                gra.DrawLine(p, poin[i], poin[i + 1]);
            }
            picBackground.Image = c_bmpFrmBackgroundCache;
            gra.Dispose();

        }

        [DllImport("gdi32.dll")]
        private static extern IntPtr CreatePolygonRgn(ref Point lpPoint, int nCount, int nPolyFillMode);

        [DllImport("user32.dll")]
        private static extern IntPtr SetWindowRgn(IntPtr hWnd, IntPtr hRgn, Boolean bReDraw);
        

        private void InformationForm_Load(object sender, EventArgs e)
        {
            cmbxCharacter.SelectedIndex = 0;
            DepartmentComboBox.SelectedIndex = 0;
            this.Left = this.Owner.Left + VideoForm.c_xInfoPicbx - 160;
            this.Top = this.Owner.Top + 40;

            this.cmbxCharacter.Focus();
            txtPhysicalCnd.Text = PublicResources.g_strPhysicalCondition;
            txtPhysicalCnd.ForeColor = Color.Gray;
            txtSelfDiscribing.Text = PublicResources.g_strSelfDiscribing;
            txtSelfDiscribing.ForeColor = Color.Gray;
        }

        private void QuitButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            if (BoyRadio.Checked)
                PublicResources.g_blUserGender = true;
            else
                PublicResources.g_blUserGender = false;
            PublicResources.g_strUserCharacter = cmbxCharacter.SelectedItem.ToString();
            PublicResources.g_strUserDepartment = DepartmentComboBox.SelectedItem.ToString();
            PublicResources.g_strImageBaseName = "Capture\\" + DateTime.Now.ToString("yyyyMMdd");
            PublicResources.g_strImageBaseName += cmbxCharacter.SelectedIndex.ToString() + DepartmentComboBox.SelectedIndex.ToString();
            PublicResources.g_strSelfDiscribing = txtSelfDiscribing.Text.Trim();
            PublicResources.g_strPhysicalCondition = txtPhysicalCnd.Text.Trim();
            WelcomeForm.g_pbResources.QuestinnairRead();
            VideoForm.g_isUserInfoDone = true;
            this.Close();
        }

        private void picBackground_MouseDown(object sender, MouseEventArgs e)
        {
            c_bMoving = true;
            c_pStart = MousePosition;
        }

        private void picBackground_MouseMove(object sender, MouseEventArgs e)
        {
            if(c_bMoving)
            {
                int x = MousePosition.X - c_pStart.X;
                int y = MousePosition.Y - c_pStart.Y;
                this.Left += x;
                this.Top += y;
                this.Owner.Left += x;
                this.Owner.Top += y;
                c_pStart = MousePosition;
            }
        }

        private void picBackground_MouseLeave(object sender, EventArgs e)
        {
            c_bMoving = false;
        }

        private void picBackground_MouseUp(object sender, MouseEventArgs e)
        {
            c_bMoving = false;
        }

        private void txtPhysicalCnd_Enter(object sender, EventArgs e)
        {
            txtPhysicalCnd.ForeColor = Color.Black;
            txtPhysicalCnd.SelectAll();
            m_TextFocus = true;
        }

        private void txtPhysicalCnd_MouseUp(object sender, MouseEventArgs e)
        {
            if (m_TextFocus)
                txtPhysicalCnd.SelectAll();
            m_TextFocus = false;
        }

        private void txtSelfDiscribing_Enter(object sender, EventArgs e)
        {
            txtSelfDiscribing.ForeColor = Color.Black;
            txtSelfDiscribing.SelectAll();
            m_TextFocus = true;
        }

        private void txtSelfDiscribing_MouseUp(object sender, MouseEventArgs e)
        {
            if (m_TextFocus)
                txtSelfDiscribing.SelectAll();
            m_TextFocus = false;
        }

        private void Control_Enter(object sender, EventArgs e)
        {
            Control c = (sender as Control);
            Graphics gra = Graphics.FromImage(c_bmpFrmBackground);
            gra.DrawImage(c_bmpFrmBackgroundCache, new Point(0, 0));
            gra.FillRectangle(Brushes.MediumPurple, 0, c.Top, Width, c.Height);
            picBackground.Image = c_bmpFrmBackground;
            gra.Dispose();
            picBackground.Refresh();
        }



    }
}
